package com.yuan.model_community.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityBuilding;
import com.yuan.model_community.domain.CommunityUnit;
import com.yuan.model_community.repository.CommunityUnitDao;
import com.yuan.model_community.service.CommunityUnitService;
import com.yuan.model_community.service.dto.CommunityUnitDto;
import com.yuan.model_community.service.mapstruct.CommunityUnitMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@Service
@RequiredArgsConstructor
public class CommunityUnitServiceImpl implements CommunityUnitService {

    private final CommunityUnitDao communityUnitDao;

    private final CommunityUnitMapper communityUnitMapper;

    @Override
    public PageResult<CommunityUnitDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<CommunityUnit> page = communityUnitDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        for (CommunityUnit communityUnit : page) {
            String communityBuildingById = communityUnitDao.findCommunityBuildingById(communityUnit.getBuildingId());
            communityUnit.setBuildingName(communityBuildingById);
        }
        return PageUtil.toPage(page.map(communityUnitMapper::toDto));
    }

    @Override
    public List<CommunityUnitDto> queryAll() {
        List<CommunityUnit> all = communityUnitDao.findAll();
        return communityUnitMapper.toDto(all);
    }

    @Override
    public List<CommunityUnitDto> queryAll(CommonQueryCriteria criteria) {
        List<CommunityUnit> users = communityUnitDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return communityUnitMapper.toDto(users);
    }

    @Override
    public void download(List<CommunityUnitDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommunityUnitDto communityInfoDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CommunityUnit resources) {
        communityUnitDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CommunityUnit resources) throws Exception {
        Optional<CommunityUnit> byId = communityUnitDao.findById(resources.getUnitId());
        if (byId.isPresent()) {
            BeanUtils.copyProperties(resources, byId.get());
            communityUnitDao.save(byId.get());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        communityUnitDao.deleteById(ids);
    }
}
